package com.it.emall.controller;

import com.it.emall.domain.AdminUser;
import com.it.emall.domain.Comments;
import com.it.emall.service.AdminUserService;
import com.it.emall.utils.JwtHelper;
import com.it.emall.utils.Md5;
import com.it.emall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台登入认证接口
 */
@RestController
@RequestMapping("/wx/admin")
public class AdminController {

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 用户登入
     * @param adminUser
     * @return
     */
    @PostMapping("/login")
    public Object login(@RequestBody AdminUser adminUser){//接收参数形式的@RequestBody

        //调用service
        Map<String,Object> json= new HashMap<>();
        AdminUser loginuser = adminUserService.login(adminUser.getUsername());

        System.out.println(loginuser.getPassword());
        String s = Md5.md5(adminUser.getPassword());
        System.out.println(s);
        if(loginuser != null && loginuser.getPassword().equals(s)){
            System.out.println("登入成功");
            Map<String,Object> data = new HashMap<>();
            data.put("token",JwtHelper.createToken(loginuser.getUserId()));
            Map<String,Object> adminInfo = new HashMap<>();
            adminInfo.put("nikeName",loginuser.getUsername());
            adminInfo.put("avatar",loginuser.getAvatar());
            //测试
            adminInfo.put("userId",loginuser.getUserId());
            data.put("adminInfo",adminInfo);
            return ResponseUtils.ok(data);
        }else {
            System.out.println("失败");
            return ResponseUtils.fail(ResponseUtils.INVALID_TOKEN,"用户名或者密码错误");
        }

    }

    /**
     * 用户注册
     * @param adminUser
     * @return
     */
    @PostMapping("/register")
    public Object register(@RequestBody AdminUser adminUser) {
        int isRegistered = adminUserService.register(adminUser);
        if (isRegistered != 0) {
            Map<String, Object> data = new HashMap<>();
            data.put("message", "注册成功");
            return ResponseUtils.ok(data);
        } else {
            return ResponseUtils.fail(ResponseUtils.INVALID_TOKEN, "用户名已存在或注册失败");
        }
    }


    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    public Object info(@RequestHeader("token") String token){
        Integer userId = JwtHelper.verifyTokenAndGetUserId(token);
        AdminUser user = adminUserService.selectUserById(userId);
        Map<String,Object> data = new HashMap<>();
        data.put("name",user.getUsername());
        data.put("avatar",user.getAvatar());

        return ResponseUtils.ok(data);


    }

    /**
     * 查询用户信息根据id
     * @param id
     * @return
     */
    @GetMapping("user")
    public Object user(@RequestParam Integer id){
        AdminUser adminUser = adminUserService.selectUserById(id);
        return ResponseUtils.ok(adminUser);
    }

    /**
     * 修改用户信息
     * @param adminUser
     * @return
     */
    @PostMapping("update")
    public Object updateUser(@RequestBody AdminUser adminUser,@RequestHeader("token") String token){
        Integer userId = JwtHelper.verifyTokenAndGetUserId(token);
        System.out.println(userId);
        System.out.println(adminUser.getUserId());
        if(userId == adminUser.getUserId()){
            int i = adminUserService.updateUserInfo(adminUser);
            return ResponseUtils.ok(i);
        }else {
            return ResponseUtils.fail(0,"id不存在");
        }
    }
    /**
     * 根据userId查询用户评论
     */

    @GetMapping("/commect/list")
    public Object selectCommectById(@RequestHeader("token") String token){
        Integer userId = JwtHelper.verifyTokenAndGetUserId(token);
        System.out.println(userId);
        List<Comments> comments = adminUserService.selectCommentsById(userId);
        return ResponseUtils.ok(comments);
    }
}
